package com.amap.demo.view;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps.AMap;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 定位 <br />
 * 主要包括，地图的定位模式
 *
 * @author ys
 */
public class LocationActivity extends AppCompatActivity implements LocationSource, AMapLocationListener, RadioGroup.OnCheckedChangeListener {

    private static final String TAG = LocationActivity.class.getSimpleName();

    @Bind(R.id.locat_map)
    MapView mapView;
    @Bind(R.id.gps_locate_button)
    RadioButton gpsLocateButton;
    @Bind(R.id.gps_follow_button)
    RadioButton gpsFollowButton;
    @Bind(R.id.gps_rotate_button)
    RadioButton gpsRotateButton;
    @Bind(R.id.gps_radio_group)
    RadioGroup gpsRadioGroup;

    private AMap aMap;
    private AMapLocationClient locationClient;
    private AMapLocationClientOption locationClientOption;
    private OnLocationChangedListener locationChangedListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_location);
        ButterKnife.bind(this);

        mapView.onCreate(savedInstanceState);

        init();
    }


    /**
     *
     */
    private void init() {
        if (null == aMap) {
            aMap = mapView.getMap();
            aMap.setLocationSource(this);
            aMap.getUiSettings().setMyLocationButtonEnabled(true);
            aMap.setMyLocationEnabled(true);
            aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
        }
        gpsRadioGroup.setOnCheckedChangeListener(this);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.gps_locate_button:
                aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
                break;
            case R.id.gps_follow_button:
                aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_FOLLOW);
                break;
            case R.id.gps_rotate_button:
                aMap.setMyLocationType(AMap.LOCATION_TYPE_MAP_ROTATE);
                break;
            default:
                aMap.setMyLocationType(AMap.LOCATION_TYPE_LOCATE);
                break;
        }
    }

    @Override
    public void onLocationChanged(AMapLocation aMapLocation) {
        if (locationChangedListener != null && aMapLocation != null) {
            if (aMapLocation != null && aMapLocation.getErrorCode() == 0) {
                locationChangedListener.onLocationChanged(aMapLocation);
            } else {
                Toast.makeText(this, "定位失败" + aMapLocation.getErrorInfo() + "\n" + aMapLocation.getErrorCode(), Toast.LENGTH_SHORT).show();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
        deactivate();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        if (null != locationClient) {
            locationClient.onDestroy();
        }
    }

    /**
     * 激活定位
     *
     * @param onLocationChangedListener
     */
    @Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        this.locationChangedListener = onLocationChangedListener;
        if (locationClient == null) {
            locationClient = new AMapLocationClient(this);
            locationClient.setLocationListener(this);

            locationClientOption = new AMapLocationClientOption();
            locationClientOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);//高精度定位模式
            locationClientOption.setGpsFirst(true);
            locationClient.setLocationOption(locationClientOption);

            locationClient.startLocation();
        }
    }

    /**
     * 结束定位
     */
    @Override
    public void deactivate() {
        locationChangedListener = null;
        if (locationClient != null) {
            locationClient.stopLocation();
            locationClient.onDestroy();
        }
        locationClient = null;
    }


}
